const app = getApp();
Page({
  data:{
    order:null,
    first:true, //是否是第一次支付
    ticketType: 0,
  },
  onLoad(params){
    const paramsObj = JSON.parse(decodeURIComponent(params.paramsStr))
    console.log(paramsObj, 'paramsObj');
    const diffTime = Math.floor((new Date(paramsObj.payTimeEnd) - new Date())); //计时器
    paramsObj.diffTime = diffTime;  // 倒计时时间
    this.initData(paramsObj)
  },
  initData(params){
    this.setData({
      order:params
    })
  },
  radioChange(e) {
    const ticketType = e.detail.value;
    const { ticketNum, ticketOriginalPrice, ticketUnitPrice }  = this.data.order;
      this.setData({
        order: {
          ...this.data.order,
          price: (ticketType ==='0' ? ticketUnitPrice : ticketOriginalPrice ),  // 单张票价
          totalPrice: ticketNum * (ticketType ==='0' ? ticketUnitPrice : ticketOriginalPrice ), // 总票价
        },
        ticketType // 购票类型
      })
  },
  //模拟支付
  payment(){
    const { ticketNum, ticketOriginalPrice, ticketUnitPrice }  = this.data.order;
    //避免重复支付
    // const paramsStr = JSON.stringify({"cinemaName":"中影万泰LUXE巨幕影城（石岩创维店）","cinemaId":"124801","hall":"4号厅","movieName":"刀尖","movieImg":"https://gw.alicdn.com/i2/O1CN01qp0IQD1SmPUNTpYeU_!!6000000002289-0-alipicbeacon.jpg_480x480Q30s150.jpg","movieId":"2537","grade":"7.5","duration":121,"time":"今天11月24日 17:10:0","price":"44.00","seat":[{"seatId":"MTI0ODAxQF9AMjUzN0BfQDE3MDA4MTcwMDBAX0A3QF9AMzZAX0AwMDAwMDAzMzg3MS02LTEwQF9AODhAX0AwQF9ANuaOkjbluqdAX0AxMjM0MTM4M0BfQDA=","seatInfo":"6排6座","columnNo":"9","rowNo":"6","index":"96"}],"loveStatus":"0","seatIds":["MTI0ODAxQF9AMjUzN0BfQDE3MDA4MTcwMDBAX0A3QF9AMzZAX0AwMDAwMDAzMzg3MS02LTEwQF9AODhAX0AwQF9ANuaOkjbluqdAX0AxMjM0MTM4M0BfQDA="],"seatNo":["6排6座"],"showId":"47578556ba2c92410007","ticketNum":1,"ticketOriginalPrice":"44.90","ticketUnitPrice":"44.00"});
    const paramsStr = JSON.stringify({
      ...this.data.order, 
      ticketType: this.data.ticketType,
      ticketTypeName:  (this.data.ticketType ==='0' ? '特惠购票' : '快速购票' ),
      price: (this.data.ticketType =='0' ? ticketUnitPrice : ticketOriginalPrice ),
      totalPrice: ticketNum * (this.data.ticketType =='0' ? ticketUnitPrice : ticketOriginalPrice ), // 总票价
    })
    wx.navigateTo({
      url: `/pages/subPages/buy-ticket-detail/buy-ticket-detail?paramsStr=${encodeURIComponent(paramsStr)}`,
    })
    if(this.data.first){
      // var that = this;
      // wx.request({
      //   url: app.globalData.url +`/movieOrder/submitMovieOrder`,
      //   method: 'POST',
      //   data:this.data.order,
      //   success(res) {
      //     if(res.data.code === 200) {
            
      //     } else {
      //       wx.showToast({
      //         title: res.data.msg,
      //       })
      //     }
      //   },
      //   fail(err) {
      //     reject(err)
      //   }
      // })
      return
      // let movieOrder = wx.getStorageSync('movieOrder') || []
      // movieOrder.unshift(this.data.order)
      // wx.setStorageSync('movieOrder', movieOrder)
      //请求后台模拟支付
      const order = this.data.order;
      wx.getStorage({
        key: 'userInfo',
        success: function (res1) {
          wx.request({
            url: app.globalData.url + '/order/addOrder',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'token': res1.data.data.token
            },
            data: {
              itemId: order.movieId,
              itemType: '电影票',
              relateId: order.timesId,
              orderId: order.orderId,
              price: order.price,
              seat: JSON.stringify(order.seat)
            },
            success(res) {
              if (res.data.state == 202) {
                wx.showModal({
                  content: '您还未登录',
                  success(res) {
                    wx.switchTab({
                      url: '/pages/user/user',
                    })
                  }
                })
              }else if (res.data.state===200){
                wx.showToast({
                  title: '支付成功',
                })
                that.setData({
                  first: false
                })
                // wx.navigateTo({
                //   url: `../movie-order-detail/movie-order-detail?paramsStr=${JSON.stringify(res.data.data)}`,
                // })
                wx.switchTab({
                  url: '/pages/user/user',
                })
              }else{
                wx.showToast({
                  title: '支付失败',
                })
              }
            }
          })
        },
        fail: function (res2) {
          wx.showModal({
            content: '您还未登录',
            success(res) {
              wx.switchTab({
                url: '../../user/user',
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '已支付',
        icon:'none'
      })
    }
  }
})